package com.neeraj.SharingArtifacts;

import java.util.Locale;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class CurLocation extends Activity implements LocationListener, OnCancelListener{
		
	static final String DEBUG_TAG = "CURRENT LOCATION";
	static final int CUSTOM_DIALOG_ID = 0;
	static final int WAIT_DIALOG_ID = 1;
			
	Bundle bun;	 
	Geocoder myGeocoder;	
//	final static int MAX_RESULT = 5;
	
	private LocationManager locationManager;
	private Location location;
	private String bestProvider;
	
    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        myGeocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        //for API Level 9 or higher
        if (!Geocoder.isPresent()){
        	Toast.makeText(getBaseContext(),
        	"Sorry! Geocoder service not Present.",
        	Toast.LENGTH_LONG).show();
        }
        showDialog(WAIT_DIALOG_ID);
 		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);     		

   		Criteria criteria = new Criteria();
   		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
  		criteria.setAccuracy(Criteria.ACCURACY_FINE);
   		bestProvider = locationManager.getBestProvider(criteria, false);

   		location = locationManager.getLastKnownLocation(bestProvider); 
   		Log.d(DEBUG_TAG, "in on create" + location.getLatitude() + ", " + location.getLongitude());
    }
    
    @Override
    protected void onResume() {
		super.onResume();		
		locationManager.requestLocationUpdates(bestProvider, 1000, 1, this);
	}

	/** Stop the updates when Activity is paused */
	@Override
	protected void onPause() {
		super.onPause();
		locationManager.removeUpdates(this);
	}
   
    	@SuppressWarnings("deprecation")
		@Override
    	public void onLocationChanged(Location loc) {
    		location = loc;
		    location.getLatitude();
		    location.getLongitude();
//		    dismissDialog(WAIT_DIALOG_ID);
		    removeDialog(WAIT_DIALOG_ID);
		    String Text = "My current location is:" + "Latitude = " + 
		    		location.getLatitude() + ",Longitude = " + location.getLongitude();
		    Toast.makeText( CurLocation.this, Text, Toast.LENGTH_SHORT).show();
    	}

    	@Override
	    public void onProviderDisabled(String provider) {
	    	Toast.makeText( CurLocation.this, "Gps Disabled", Toast.LENGTH_SHORT ).show();
	    }
	
	    @Override
	    public void onProviderEnabled(String provider) {
	    	Toast.makeText( CurLocation.this, "Gps Enabled", Toast.LENGTH_SHORT).show();
	    }
	    
	    @Override
	    public void onStatusChanged(String provider, int status, Bundle extras){}    

  
	    void CatchError(String Exception) {	    	
	    	Dialog diag=new Dialog(this);
	    	diag.setTitle("Updating Database");
	    	TextView txt=new TextView(this);
	    	txt.setText(Exception);
	    	diag.setContentView(txt);
	    	diag.show();
	    }	
	    
	    protected Dialog onCreateDialog(int id) {
	    	 // TODO Auto-generated method stub
	    	 Dialog alertDialog = null;;
	    	    switch(id) {
	    	    case WAIT_DIALOG_ID:
	    			alertDialog = new ProgressDialog(this);
	    			((ProgressDialog)alertDialog).setMessage("Waiting for GPS location...");
	    			alertDialog.setCancelable(true);
	    			alertDialog.setOnCancelListener(this);
	    			break;
	    	    }
	    	    return alertDialog;
	    }
	    
		@SuppressWarnings("deprecation")
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			removeDialog(WAIT_DIALOG_ID);
		}	    
		
		double getLatitude() {
			return location.getLatitude();
		}
		
		double getLongitude() {
			return location.getLongitude();
		}
		
		double getAltitude() {
			return location.getAltitude();
		}
		
		long getTime() {
			return location.getTime();
		}
}